// pages/choiceness/choiceness.js

import newData from '../../Datas/mgtv.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    loadtxt: '正在加载',
    currentId: '1001',
    banners: [],
    rollLink: [],
    avatorText: [],
    newList: [],
    selection: [{
        name: '精选',
        id: '1001'
      }, {
        name: '黄金单身汉',
        id: '1032'
      },
      {
        name: '综艺',
        id: '1003'
      }, {
        name: '电视剧',
        id: '1004'
      },
      {
        name: '电影',
        id: '1005'
      }, {
        name: '少儿',
        id: '1021'
      }
    ]
  },

  isEmptyObject: function(e) {
    //判断Object对象是否为空
    let t;

    for (t in e)
      return !1;
    return !0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    // loading:true;
    // loadtxt:'';
    // var _this=this;
    // const reqUrl = "http://127.0.0.1:8080/mgtvwechat/Datas/"
    const reqUrl = "http://192.168.1.101:8080/mgtvwechat/Datas/"
    // wx.request({
    //   url: reqUrl + this.data.currentId + '.json',
    //   success:function(result){
    //     console.log(result)
    //   }
    // })



    let _this = this;
    let param = {
      API_URL: reqUrl + this.data.currentId + '.json'
    }

    newData.result(param).then(data => {
      let datas = data.data.data,
        bannerData = [],
        AvatorData = [],
        lists = [],
        RollData = [],
        obj = {};
      for (let i = 0; i < datas.length - 10; i++) {
        // -20 是因为接口是按照M站的接口给的，为了避免一些接口数据格式问题,实际开发不需要-10
        if (datas[i].type == 'banner') {
          bannerData = datas[i].templateData;
        }
        if (datas[i].type == 'roundAvatorText') {
          AvatorData = datas[i].templateData;
        }

        if (datas[i].type == 'textRollLink') {
          RollData = datas[i].templateData;
          console.log(datas[i].templateData)
        }

        if (datas[i].type == 'title' ||
          datas[i].type == 'normalLandScape' ||
          datas[i].type == 'largeLandScapeNodesc' ||
          datas[i].type == 'textMoreLink' ||
          datas[i].type == 'normalLandScapeNodesc' &&
          datas[i].templateData.length) {

          if (datas[i].type == 'title') {

            if (!_this.isEmptyObject(obj)) {
              lists.push(obj);
              obj = {};
              obj.indexs = i;
            }

            if (datas[i + 1].type == 'largeLandScapeNodesc') {
              obj.type = 'big';
              obj.name1 = datas[i].templateData[0].name;
              obj.name2 = datas[i + 1].templateData[0].name;
              obj.bigPic = datas[i + 1].templateData[0].picUrl;
            } else {
              obj.title = datas[i].templateData[0].name;
              obj.type = 'small';

            }

            obj.more = datas[i].templateData[0]['jumpChannel'] ? true : false;
            obj.list = [];

          }

          if (datas[i].type == 'textMoreLink') {
            obj.links = datas[i].templateData[0].name;
          }

          if (datas[i].type == 'normalLandScape' ||
            datas[i].type == 'normalLandScapeNodesc') {
            obj.list = obj.list.concat(datas[i].templateData);
          }

        }
      }

      _this.setData({
        loading: true,
        loadtxt: '来了来了',
        banners: bannerData,
        avatorText: AvatorData,
        rollLink: RollData,
        newList: Object.assign([], lists)
      })
    })

  },
  handleTab: function(e) {
    console.log(e);
    let id = e.currentTarget.id;
    console.log(id);
    if (id) {
      // 重新赋值currentId
      this.setData({
        currentId: id
      })
      // 界面重新加载
      this.onLoad()
    }
  }
})